package com.ciquan.server.controller;

import com.ciquan.server.bean.UserInfo;
import com.ciquan.server.bean.UserRecommend;
import com.ciquan.server.service.UserInfoService;
import com.ciquan.server.service.UserRecommendService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by kakalee on 15/3/27.
 */
@Controller
@RequestMapping("/userRecommend")
public class UserRecommendController extends AbstractController {

    @Resource
    private UserRecommendService userRecommendService;

    @Resource
    private UserInfoService userInfoService;

    @RequestMapping("/list")
    public ModelAndView list(ModelMap modelMap) {
        List<UserRecommend> userRecommends = userRecommendService.selectList();
        modelMap.put("userRecommends", userRecommends);
        return new ModelAndView("userRecommendList", modelMap);
    }

    @RequestMapping("add")
    public ModelAndView add(ModelMap modelMap) {
        List<UserInfo> userInfos = userInfoService.selectMerchant();
        modelMap.put("userInfos", userInfos);
        return new ModelAndView("addUserRecommend", modelMap);
    }

    @RequestMapping("saveAdd")
    public ModelAndView saveAdd(HttpServletRequest request) {
        int userId = ServletRequestUtils.getIntParameter(request, "userId", 0);
        UserRecommend userRecommend = new UserRecommend();
        userRecommend.setUserId(userId);
        userRecommendService.insert(userRecommend);
        return new ModelAndView("redirect:/userRecommend/list");
    }

    @RequestMapping("edit")
    public ModelAndView edit(HttpServletRequest request, ModelMap modelMap) {
        int id = ServletRequestUtils.getIntParameter(request, "id", 0);
        int userId = ServletRequestUtils.getIntParameter(request, "userId", 0);
        UserRecommend userRecommend = new UserRecommend();
        userRecommend.setId(id);
        userRecommend.setUserId(userId);
        List<UserInfo> userInfos = userInfoService.selectMerchant();

        modelMap.put("userRecommend", userRecommend);
        modelMap.put("userInfos", userInfos);
        return new ModelAndView("editUserRecommend", modelMap);
    }

    @RequestMapping("saveEdit")
    public ModelAndView saveEdit(HttpServletRequest request) {
        int id = ServletRequestUtils.getIntParameter(request, "id", 0);
        int userId = ServletRequestUtils.getIntParameter(request, "userId", 0);
        UserRecommend userRecommend = new UserRecommend();
        userRecommend.setId(id);
        userRecommend.setUserId(userId);
        userRecommendService.update(userRecommend);
        return new ModelAndView("redirect:/userRecommend/list");
    }
}
